//
//  ViewController.m
//  婚礼纪  首页
//
//  Created by 太阳 on 15/5/15.
//  Copyright (c) 2015年 三人. All rights reserved.
//

#import "ViewController.h"
#import "RecommendCell.h"
#import "RecommendModel.h"
#import "ConnectionModel.h"
#import "CustomHeaderView.h"
#import "DiscountCell.h"
#import "tagCell.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation ViewController
-(void)awakeFromNib
{
    [super awakeFromNib];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    [self.view setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.8]];
    [self requestNet];
    [self seachaction];
    [self createtablrview];
}
- (void)requestNet
{
    
}
//创建tableview
- (void)createtablrview
{
    //题头什么的
    self.navigationController.navigationBar.translucent = NO;
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 30)];
    [button setTitle:@"全国" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithRed:0.9 green:0.2 blue:0.2 alpha:1] forState:UIControlStateNormal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(seachaction)];

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 30)];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];
    label.text = @"婚礼纪";
    label.font = [UIFont systemFontOfSize:20];
    label.textColor = [UIColor colorWithRed:0.9 green:0.2 blue:0.2 alpha:1];
    [view addSubview:label];
    self.navigationItem.titleView = view;
    
    
    //  tableview的设置
    self.mytableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height) style:UITableViewStylePlain];
    self.mytableView.delegate = self;
    self.mytableView.dataSource = self;
    [self.mytableView setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.6]];
    [self.view addSubview:self.mytableView];
    //申请释放池
    [self.mytableView registerClass:[DiscountCell class] forCellReuseIdentifier:@"discountcell"];
    [self.mytableView registerClass:[RecommendCell class] forCellReuseIdentifier:@"recommendcell"];
    [self.mytableView registerClass:[tagCell class] forCellReuseIdentifier:@"tagcell"];
    
    CustomHeaderView *header = [[CustomHeaderView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width * 1.18)];
    self.mytableView.tableHeaderView = header;
}
//网络请求
- (void)seachaction
{
    [ConnectionModel connectDoneWithStr:@"http://hunliji.com/v2/api/app/works?page=1&per_page=20&commodity_type=1&first_query_time=2015-05-15T09%3A54%3A22Z" block:^(NSData *data) {
        self.recommmendArray = [NSMutableArray array];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [dic objectForKey:@"works"];
        for (NSDictionary *second in array) {
            RecommendModel *model = [[RecommendModel alloc]init];
            [model setValuesForKeysWithDictionary:second];
            NSDictionary *mer = [second objectForKey:@"merchant"];
            NSString *str = [mer objectForKey:@"name"];
            [model setValue:str forKey:@"name"];
            [self.recommmendArray addObject:model];
            
            [self.mytableView reloadData];
        }
    }];
    
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0 ) {
        return 1;
    }
    if (section == 1) {
        return 1;
    }
    else {
        return [self.recommmendArray count];
    }
    return 0;
}
//cell  建立
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        DiscountCell *cell = [tableView dequeueReusableCellWithIdentifier:@"discountcell"];
        return cell;
    }
    if (indexPath.section == 1){
        
        tagCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tagcell"];
        return cell;
    }
    
    if (indexPath.section == 2) {
    
    
    RecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"recommendcell"];
    RecommendModel *model = [[RecommendModel alloc]init];
    model = [self.recommmendArray objectAtIndex:indexPath.row];
    [cell cellBuildingWithModel:model];
    
    return cell;
    }
    return nil;
}

//题头 设置
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    if (section == 0) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
        [view setBackgroundColor:[UIColor colorWithWhite:0.3 alpha:0.4]];
        UIView *two = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 32)];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 3, 100, 30)];
        label.text = @"超值折扣";
        label.font = [UIFont systemFontOfSize:19];
        label.textColor = [UIColor colorWithWhite:0.3 alpha:0.8];
        [two addSubview:label];
        
        UILabel *twolabel = [[UILabel alloc]initWithFrame:CGRectMake(300, 3, 60, 25)];
//        [twolabel setBackgroundColor:[UIColor yellowColor]];
        twolabel.text = @"更多 >";
        twolabel.textColor = [UIColor colorWithWhite:0.3 alpha:0.6];
        
        [two addSubview:twolabel];
        
        [two setBackgroundColor:[UIColor whiteColor]];
        [view addSubview:two];
        return view;
    }
    if (section == 1)
    {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
        [view setBackgroundColor:[UIColor colorWithWhite:0.3 alpha:0.4]];
        UIView *two = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 32)];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 3, 100, 30)];
        label.text = @"标签推荐";
        label.font = [UIFont systemFontOfSize:19];
        label.textColor = [UIColor colorWithWhite:0.3 alpha:0.8];
        [two addSubview:label];
        
        UILabel *twolabel = [[UILabel alloc]initWithFrame:CGRectMake(300, 3, 60, 25)];
        //        [twolabel setBackgroundColor:[UIColor yellowColor]];
        twolabel.text = @"更多 >";
        twolabel.textColor = [UIColor colorWithWhite:0.3 alpha:0.6];
        
        [two addSubview:twolabel];
        
        [two setBackgroundColor:[UIColor whiteColor]];
        [view addSubview:two];
        return view;
    }
    if(section == 2){
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
        [view setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.8]];
        UILabel *lebel = [[UILabel alloc]initWithFrame:CGRectMake(0, 3, self.view.frame.size.width, 30)];
        lebel.text = @"––––––– 推荐作品 –––––––";
        lebel.font = [UIFont systemFontOfSize:20];
        lebel.textAlignment = NSTextAlignmentCenter;
        lebel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.6];
        [view addSubview:lebel];
        return view;
    }
    return nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0 ) {
        return 33;
    }
    if (section == 1) {
        return 33;
    }
    else {
        return 40;
    }
    return 0;
}

//row 高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 ) {
        return 260;
    }
    if (indexPath.section == 1) {
        return 250;
    }
    else {
        return 290;
    }
    
    
    return 290;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 40;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
@end
